<?php
/**
 * CakePOWER
 * Package Bootstrapping
 * 
 * @author:			Marco Pegoraro
 * @mail:			marco(dot)pegoraro(at)gmail(dot)com
 * 
 * 21 September 2009
 * This is the core bootstrap for CakePHP.
 * It define a lot information about the CakePOWER basic configuration such plugins rules.
 */

# It define the base path for CakePOWER package used to load CakePOWER related resources.         #
if ( !defined('POWER') ) 				define( 'POWER', 					dirname(__FILE__).DS );


/**
 * Load Static CakePOWER's Configuration rules
 * ------------------------------------------------------------------------------------------------
 * This file define constants about CakePOWER folder organization and it is necessary to the rest
 * of the configuration process.
 */
___powerConfig( 'power', 'default' );




/**
 * Load Static Libraries
 * ------------------------------------------------------------------------------------------------
 */
// CakePHP's libraries.                                                                           #
App::import('Core', 'Sanitize');

// CakePOWER's libraries.                                                                         #
___powerLib( 'uth' );
___powerLib( 'dac' );
___powerLib( 'extension_manager' );

ExtensionManager::call('bootstrapPOWER');




/**
 * Load Static CakePOWER's Configuration rules
 * ------------------------------------------------------------------------------------------------
 */
___powerConfig( 'cache',	'default' );
___powerConfig( 'lang',		'default' );
___powerConfig( 'plugins',	'default' );
___powerConfig( 'dac',		'default' );
___powerConfig( 'global', 	'config' );

ExtensionManager::call('bootstrapConfigPOWER');




/**
 * Some DAC manipulations about dependencies
 * ------------------------------------------------------------------------------------------------ 
 */

if ( !DAC::read('power.cache.enabled') ) {
	
	// Globally deactivation of cache settings.                                                   #
	DAC::write( 'power.plugins.cache.enabled', 	DAC::read('power.cache.enabled') );
	DAC::write( 'power.dac.cache.enabled', 		DAC::read('power.cache.enabled') );
	
}

// Plugin's repositories settings.                                                                #
if ( !DAC::isEmpty('power.plugins.setRepositories') && DAC::isArray('power.plugins.setRepositories') ) {
	DAC::write( 'power.plugins.repositories', DAC::read('power.plugins.setRepositories') );
}

ExtensionManager::call('bootstrapDacPOWER');




// --- start writing DAC on Bootstrap.                                                            #

/**
 * Explore plugins folder to store some plugins lists into the DAC.
 * ------------------------------------------------------------------------------------------------
 */
___powerInc( 'plugins.explorer' );

/**
 * Url Requested Language Management.
 */
___powerInc( 'lang' );

/**
 * Initialization of DAC values.
 */
___powerInc('dac.init');

/**
 * Plugin's initialization process.
 * ------------------------------------------------------------------------------------------------
 */
___powerInc( 'plugins.bootstrap' );
___powerInc( 'plugins.extends' );

// --- end writing DAC on Bootstrap.                                                              #


/**
 * Extends CakePHP's search paths.
 * This allows plugins to heavvy integrates into application.
 */
$viewPaths 				= DAC::read('sys.repositories.views');
$helperPaths 			= DAC::read('sys.repositories.helpers');

$controllerPaths 		= DAC::read('sys.repositories.controllers');
$componentPaths 		= DAC::read('sys.repositories.components');

$modelPaths 			= DAC::read('sys.repositories.models');
$behaviorPaths 			= DAC::read('sys.repositories.behaviors');

$pluginPaths 			= DAC::read('sys.repositories.plugins');
$vendorPaths 			= DAC::read('sys.repositories.vendors');





/**
 * Global bootstrap's ExtensionPoint.
 * Here plugins can alter the paths configuration!
 */
ExtensionManager::call('bootstrapPathsPOWER', &$viewPaths, &$helperPaths, &$controllerPaths, &$componentPaths, &$modelPaths, &$behaviorPaths, &$pluginPaths, &$vendorPaths );




#DAC::debug();
#debug(Configure::read('power'));
#die();
















###################################################################################################
### CakePOWER Integration Functions.                                                            ###
### These function facilitates the integration coding.                                          ###
### One developer man can use these methods at the top of some particular files to teach        ###
### CakePHP to use CakePOWER.                                                                   ###
###################################################################################################

function powerRoutes() {
	
	___powerResource( POWER_ROUTES, true, 'CakePOWER routing library not found.{debug-path}' );
	
} // EndOf: "powerRoutes()" #######################################################################

function powerAppController() {
	
	___powerResource( POWER_APP_CONTROLLER, 	true, 'CakePOWER AppController not found.{debug-path}' );
	___powerResource( POWER_APP_COMPONENT, 		true, 'CakePOWER AppComponent not found.{debug-path}' );
	___powerResource( POWER_APP_HELPER, 		true, 'CakePOWER AppHelper not found.{debug-path}' );
	
} // EndOf: "powerAppController()" ################################################################

function powerAppModel() {
	
	___powerResource( POWER_APP_MODEL, 			true, 'CakePOWER AppModel not found.{debug-path}' );
	___powerResource( POWER_APP_BEHAVIOR, 		true, 'CakePOWER AppBehavior not found.{debug-path}' );
	
} // EndOf: "powerAppModel()" #####################################################################







###################################################################################################
### CORE FUNCTIONS                                                                              ###
### These are CakePOWER core functions shared with the entire application.                      ###
###################################################################################################

/**
 * Load a configuration file looking booth the APP/CONFIG folder (optional) and the core
 * folder (required).
 * 
 * The "mode" settings tell the application how to load App configuration:
 * 
 * - default: App's configuration file is loaded BEFORE the core configuration file.
 *            Core file contains only default values for the application.
 *            
 * - config:  App's configuration file is loaded AFTER the core configuration file.
 *            La configurazione principale contiene valori destinati ad essere sovrascritti.
 * 
 * @return 
 * @param string $configName It is the name of required configuration file.
 * @param string $mode It is the way configuration files are requested.
 */
function ___powerConfig( $configName, $mode = '' ) {
	
	// Build paths for configuration files.                                                       #
	$resourcePath	= POWER . 'config' . DS .  $configName .'.php';
	$appResourcePath 	= CONFIGS . $configName .'.php';
	
	// Application level search (default mode).                                                   #
	if ( $mode == 'default' && file_exists($appResourcePath) ) require_once( $appResourcePath );
	
	// Core level search.                                                                         #
	___powerResource( $resourcePath, true, 'Missing "'.strtoupper($configName).'" configuration file.{debug-path}' );
	
	// Application level search (configuration mode).                                             #
	if ( $mode == 'config' && file_exists($appResourcePath) ) require_once( $appResourcePath );
	
} // EndOf: "___powerConfig()" ####################################################################

/**
 * CakePOWER library inclusion service.
 * @return 
 * @param object $libName
 */
function ___powerLib( $libName ) {
	
	$resourcePath = POWER_LIB . 'lib.'.$libName.'.php';
	$errorMessage = strtoupper($libName) .' library not found.{debug-path}';
	
	___powerResource( $resourcePath, true, $errorMessage );
	
} // EndOf: "___powerLib()" #######################################################################

/**
 * CakePOWER library inclusion service.
 * @return 
 * @param object $libName
 */
function ___powerInc( $resourceName ) {
	
	$resourcePath = POWER_INC . $resourceName.'.php';
	$errorMessage = strtoupper($resourceName) .' inc-library not found.{debug-path}';
	
	___powerResource( $resourcePath, true, $errorMessage );
	
} // EndOf: "___powerInc()" #######################################################################

/**
 * Resource inclusion service with default error string.
 * 
 * @return 
 * @param object $resourcePath
 * @param object $once[optional]
 * @param object $errorMessage[optional]
 */
function ___powerResource( $resourcePath, $once = true, $errorMessage = '' ) {
	
	// Set the default error string wrapper.                                                      #
	$errorTemplate = '<div align="center" style="font-family:verdana;font-size:8pt;color:#666;"><span style="color:red;font-weight:bold;display:block;font-size:20pt;margin-bottom:20px;">:-(</span>{message}<br><a href="http://cakepower.juniorcms.com" style="color:blue;" title="Open Source CMS, CakePHP CMS">JuniorCMS.com</a></div>';
	
	// Set the default error message.                                                             #
	if ( empty($errorMessage) ) $errorMessage = '"'.$resourcePath.'" not found.';
	
	// Set the debug-path value.                                                                  #
	$debugPath = '';
	if ( Configure::read('debug') > 0 ) $debugPath = '<br />{path}';
	
	switch ( $once ) {
		
		case true:
			$includeResult = @include_once( $resourcePath );
			break;
			
		default:
			$includeResult = @include( $resourcePath );
		
	}
	
	if ( !$includeResult ) die( str_replace( array('{message}','{debug-path}','{path}'), array($errorMessage,$debugPath,$resourcePath), $errorTemplate ) );
	
} // EndOf: "___powerResource()" ##################################################################


/**
 * Common usefull methods
 * @return 
 */
function ln() {
	
	if ( Configure::read('debug') > 0 )	return "\r\n";
	
} // EndOf: "ln()" ################################################################################

function t() {
	
	if ( Configure::read('debug') > 0 ) return "\t";
	
} // EndOf: "t()" #################################################################################

?>